const app = getApp()
var util = require('../../../../../utils/util.js');
var brandImg, brandModelId, userCode, brandModelName, goBack, _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workHour: '',
    buttonClicked: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this;
    if (options) {
      brandImg = options.brandImg;
      brandModelId = options.brandModelId;
      userCode = options.userCode;
      brandModelName = options.brandModelName;
      goBack =  options.goBack
    }
    _this.setData({
      brandImg:brandImg,
      brandModelId:brandModelId,
      userCode:userCode,
      brandModelName:brandModelName
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  getWorkHour:function(e){
    var workHour = e.detail.value;
    if (isNaN(workHour/1)){
      util.alertMsg('警告', '请输入数字', function () {
        console.log('用户点击确定')
      }, function () {
        console.log('用户点击取消')
      });
      _this.setData({
        workHour: ''
      })
      return false;
    }
    _this.setData({
      workHour: workHour
    })
  },
  // 添加机型
  addBrandModel(e) {
    var workHour = _this.data.workHour;
    // 检测是否为空
    if (workHour == '' || workHour==null){
      util.alertMsg('警告', '请输入工时', function () {
        console.log('用户点击确定')
      }, function () {
        console.log('用户点击取消')
      });
    }else{
      app.addBrandModel(userCode, brandModelId, workHour, function (res) {
        if (res.message == 'repeat') {
          util.alertMsg('警告', '该机型已经添加过', function () {
            console.log('用户点击确定')
          }, function () {
            console.log('用户点击取消')
          });
          return;
        }
        if (res.status == 'success') {
          _this.initUserBrandModelFun(userCode);
        }
      })
    }
    
  },
  // 修改绑定机型数据
  initUserBrandModelFun(code) {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    if (typeof code == 'string') {
      var userCode = code
    } else {
      var userCode = code.code
    }
    app.globalData.xieYouUserInfoRoot.userBrandsModel = [];
    app.initUserBrandModel(userCode, function (data) {
      console.log('app初始化绑定机型');
      for (var i = 0; i < data.length; i++) {
        //将用户机型数据存入到全局数组
        app.globalData.xieYouUserInfoRoot.userBrandsModel.push(data[i]);
        // 将选中的机型显示到选择框中
        if (data[i].selected == 1) {
          console.log(data[i]);
          app.globalData.xieYouUserInfoRoot.userBrandModel = data[i]
        }
      }
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];   //当前页面
      var prevPage = pages[pages.length - 3];  //上一个页面
      var prevPageTow = pages[pages.length - 4];
      prevPage.setData({
        userBrandsModel: app.globalData.xieYouUserInfoRoot.userBrandsModel
      })
      if (goBack == 'index') {
        //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
        prevPageTow.setData({
          userBrandModel: app.globalData.xieYouUserInfoRoot.userBrandModel,
          brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
          brandModelName: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.name
        })
      } else if (goBack == 'recommend') {
        prevPageTow.setData({
          brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
          brandModelName: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.name
        })
        var prevPageThree = pages[pages.length - 5];
        prevPageThree.setData({
          userBrandModel: app.globalData.xieYouUserInfoRoot.userBrandModel,
          brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
          brandModelName: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.name
        })
      } else if (goBack == 'shopList') {
        prevPage.setData({
          brandModel: app.globalData.xieYouUserInfoRoot.userBrandModel,
          brandsModelList: app.globalData.xieYouUserInfoRoot.userBrandsModel,
        })
        var prevPageThree = pages[0];
        prevPageThree.setData({
          userBrandModel: app.globalData.xieYouUserInfoRoot.userBrandModel,
          brandLogo: app.globalData.xieYouUserInfoRoot.userBrandModel.brand.brandLogo,
          brandModelName: app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.name
        })
      }
      if (!_this.data.buttonClicked) {
        util.buttonClicked(_this);
        wx.navigateBack({
          delta: 2
        })
      }
      // wx.redirectTo({ 
      //   url: '../user-model?time=2'
      // })

    });
  },
  // 重新选择机型
  chooseNewBrand:function(){
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateBack({
        delta: 1
      })
    }
    
  }
})

